package com.chaoma.web.controller.cm;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chaoma.common.core.controller.BaseController;
import com.chaoma.common.core.domain.AjaxResult;
import com.chaoma.system.domain.CmAppVersion;
import com.chaoma.system.service.ICmAppVersionService;
import com.chaoma.common.utils.poi.ExcelUtil;
import com.chaoma.common.core.page.TableDataInfo;

/**
 * APP版本管理Controller
 *
 * @author chaoma
 * @date 2023-10-07
 */
@RestController
@RequestMapping("/system/cmAppversion")
public class CmAppVersionController extends BaseController {
    @Autowired
    private ICmAppVersionService cmAppVersionService;

    /**
     * 查询APP版本管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmAppVersion cmAppVersion) {
        startPage();
        List<CmAppVersion> list = cmAppVersionService.selectCmAppVersionList(cmAppVersion);
        return getDataTable(list);
    }

    /**
     * 导出APP版本管理列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmAppVersion cmAppVersion) {
        List<CmAppVersion> list = cmAppVersionService.selectCmAppVersionList(cmAppVersion);
        ExcelUtil<CmAppVersion> util = new ExcelUtil<CmAppVersion>(CmAppVersion.class);
        util.exportExcel(response, list, "APP版本管理数据");
    }

    /**
     * 获取APP版本管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cmAppVersionService.selectCmAppVersionById(id));
    }

    /**
     * 新增APP版本管理
     */
    @PostMapping
    public AjaxResult add(@RequestBody CmAppVersion cmAppVersion) {
        cmAppVersion.setAddTime(new Date());
        return toAjax(cmAppVersionService.insertCmAppVersion(cmAppVersion));
    }

    /**
     * 修改APP版本管理
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CmAppVersion cmAppVersion) {
        return toAjax(cmAppVersionService.updateCmAppVersion(cmAppVersion));
    }

    /**
     * 删除APP版本管理
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmAppVersionService.deleteCmAppVersionByIds(ids));
    }

    @GetMapping("checkVersion")
    public AjaxResult checkVersion(String currentVersion) {

        CmAppVersion appVersion = cmAppVersionService.selectLatestVersion();
        String latestVersion = appVersion.getAppVersion();
        Map<String, Object> map = new HashMap<>();
        if (compareVersions(currentVersion, latestVersion) < 0) {
            map.put("downloadUrl", appVersion.getDownloadUrl());
            map.put("isUpdate", true);
            return AjaxResult.success("需要更新！", map);
        } else {
            map.put("downloadUrl","");
            map.put("isUpdate", false);
            return AjaxResult.success("已经是最新版本！", map);
        }
    }

    private int compareVersions(String v1, String v2) {
        String[] version1 = v1.split("\\.");
        String[] version2 = v2.split("\\.");

        int length = Math.max(version1.length, version2.length);
        for (int i = 0; i < length; i++) {
            int v1Part = i < version1.length ? Integer.parseInt(version1[i]) : 0;
            int v2Part = i < version2.length ? Integer.parseInt(version2[i]) : 0;

            if (v1Part < v2Part) {
                return -1;
            } else if (v1Part > v2Part) {
                return 1;
            }
        }

        return 0;
    }

}
